$(document).ready(function(){
	setDialog();
	closeDialog();
	//初始化表格
	$('#dataGridTable').datagrid({
		title:"菜单管理",
		method:"get",
		dataType: 'json',
		pageSize:20,
		pageList:[10,20,30,40],
		columns:[[ 
           {field:'resourceName',title:'菜单名称',width:250,align:'center'}, 
           {field:'resourceIco',title:'菜单图标',width:100,align:'center'},
           {field:'resourceUrl',title:'菜单url',width:100,align:'center'},
           {field:'resourceIndex',title:'菜单序号',width:100,align:'center'},
           {field:'resourceParents',title:'父菜单',width:100,align:'center'}
         ]]
	});     
});



//删除角色
function delRole(){
	if($('#dataGridTable').datagrid('getSelected')){
		var ids = [];
		var cods = [];
		var selectedRow = $('#dataGridTable').datagrid('getSelections');
		for(var i=0;i<selectedRow.length;i++){
			ids.push(selectedRow[i].resourceId);
			cods.push(selectedRow[i].resourceName);
		}
		var roleId = ids.join(',');
		$.messager.confirm('删除提示', '   删除菜单资源后，它对应的用户、菜单的关系都将删除，你确定删除下列菜单吗?<br/><center>'+cods.join(',')+'</center>', function(r){
				if (r){
					var url = './menu/deleteUamsResourceByResourceId.do?resourceId='+resourceId;
					$.ajax({
						async : false,
						cache:false,
						type: 'post',
						dataType : "json",
						url:url,
						error: function () {
							sysErrorShow('请求失败');
						},success:function(data){
							if(data.code == 200){
								sysMsgShow("删除成功!");
								query();
								closeDialog();
							}else{
								messgage = data.msg;
							}
							reloadTable();
						}
					});
				}
		});
	}
	else
	{
		noSelectData();
	}
}

//查询
function query(){
	var resourceName = $("#search_roleName").textbox("getValue");
//	var systemCode = $("#roleInfoDialog_sysNameFilt").combobox("getValue"); 
	var queryParams = {resourceName:resourceName};
	$('#dataGridTable').datagrid('load',queryParams);
	$("#dataGridTable").datagrid("unselectAll");
}

/**--------------操作模态窗------------------**/
//初始化模态窗
function setDialog(){
	$('#roleInfoDialog').dialog({
		modal: true,
		collapsible : true,
		resizable : true,
		onClose : function(){
			resetDialog();
		}
	});
}

//打开添加模窗
function openDialog_add(){
	$('#roleInfoDialog').dialog({
		closed: false,
		modal:true,
        title: "新增菜单",
        height:170,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: saveUamsRole
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
            	$('#roleInfoDialog').dialog('close');                 
            }
        }]
    });
}

//保存角色
function saveUamsRole(){
	var resourceId=$("#roleInfoDialog_roleId").val();
	var resourceName = $("#roleInfoDialog_roleName").textbox('getValue');
	var resourceIco = $("#roleInfoDialog_resourceIco").textbox('getValue');
	var resourceUrl = $("#roleInfoDialog_resourceUrl").textbox('getValue');
	var resourceIndex = $("#roleInfoDialog_resourceIndex").textbox('getValue');
	var resourceParents = $("#roleInfoDialog_resourceParents").textbox('getValue');
//	var systemCode=$("#roleInfoDialog_sysName").combobox('getValue');
	if(resourceName==null||resourceName==''){
		sysMsgShow("菜单名称不能为空");
		return;
	}else{
		$.ajax({
			async : false,
			cache:false,
			type: 'POST',
			dataType : "json",
			data : {
				"resourceId" : resourceId,
//				"systemCode" : systemCode,
				"resourceName" : resourceName,
				"resourceIco" : resourceIco,
				"resourceUrl" : resourceUrl,
				"resourceIndex" : resourceIndex,
				"resourceParents" : resourceParents
				},
			url: './menu/createUamsResource.do',//请求的action路径
			error: function () {//请求失败处理函数
				sysErrorShow('请求失败');
			},success:function(data){
				var messgage = resourceId?"修改成功!":"添加成功!";
				if(data.code == 200){//未返回任何消息表示添加成功
					query();//
					closeDialog();
				}else{//返回异常信息
					messgage = data.msg;
				}
				sysMsgShow(messgage);
			}
		});
	}	
}

//打开编辑模窗
function openDialog_edit(){ 
	if($('#dataGridTable').datagrid('getSelected')){
		var resourceId = $('#dataGridTable').datagrid('getSelections')[0].resourceId;
		var row = $('#dataGridTable').datagrid('getSelections')[0];
		if($('#dataGridTable').datagrid('getSelected')){
			var row = $('#dataGridTable').datagrid('getSelections')[0];
			$("#roleInfoDialog_roleId").val(resourceId);
			$("#roleInfoDialog_roleName").textbox('setValue',row.resourceName);
			$("#roleInfoDialog_resourceIco").textbox('setValue',row.resourceIco);
			$("#roleInfoDialog_resourceUrl").textbox('setValue',row.resourceUrl);
			$("#roleInfoDialog_resourceIndex").textbox('setValue',row.resourceIndex);
			$("#roleInfoDialog_resourceParents").textbox('setValue',row.resourceParents);
//			$("#roleInfoDialog_sysName").combobox('setValue',json.object.systemCode);
			openDialog_add();
			$('#roleInfoDialog').dialog('setTitle','编辑菜单'); 
		}
	}else
	{
		noSelectData();
	}
}

/**--------------操作弹出框------------------**/
//关闭模窗
function closeDialog(){
	$('#roleInfoDialog').dialog('close');
}
//清空控件值
function resetDialog(){
	$("#roleInfoDialog_roleId").val('');
	$("#roleInfoDialog_roleName").textbox('setValue','');
	$("#roleInfoDialog_resourceIco").textbox('setValue','');
	$("#roleInfoDialog_resourceUrl").textbox('setValue','');
	$("#roleInfoDialog_resourceIndex").textbox('setValue','');
	$("#roleInfoDialog_resourceParents").textbox('setValue','')
//	$("#roleInfoDialog_sysName").combobox('setValue','');
}
//刷新表格
function reloadTable(){
	$('#dataGridTable').datagrid('reload');
}
//刷新
function refresh()
{
	$("#roleName").textbox('setValue','');
//	$("#roleInfoDialog_sysNameFilt").combobox('setValue','');
	query();
}
